package com.tp03g03;

import java.util.Date;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

public class TabVistaAlarmasActivity extends Activity {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	private NotificationManager mNM;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.tabalarmas);

		Button buttonConfigurarAlarma = (Button) findViewById(R.id.btnAgregarAlarma);
		mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

	}

	public void ConfigurarAlarma(View v) {
		// Usar Shared Preference
		// Pisar config anterior
		// Cancelar Alarma Anterior si existen
		// Configurar alarma en Alarm manager.

		startAlarm();
		
		Toast.makeText(this, R.string.SuAlarmaFueConfigurada,
				Toast.LENGTH_SHORT).show();
	}

	private void startAlarm() {
		Spinner cmbMotivoAlarma = (Spinner) findViewById(R.id.cmbMotivoAlarma);
		
		SharedPreferences settings = getSharedPreferences(Constantes.PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		
		editor.putString("MensajeAlarma", cmbMotivoAlarma.getSelectedItem().toString());
		
		editor.commit();
		
		AlarmManager alarmManager = (AlarmManager) this
				.getSystemService(this.ALARM_SERVICE);

		DatePicker dpF = (DatePicker) findViewById(R.id.dpFecha);
		TimePicker dpH = (TimePicker) findViewById(R.id.tpHora);

		Date dt = new Date(dpF.getYear() - 1900, dpF.getMonth(),
				dpF.getDayOfMonth());
		dt.setHours(dpH.getCurrentHour());
		dt.setMinutes(dpH.getCurrentMinute());

		Intent intent = new Intent(this, ReminderReceiver.class);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
				0);
		alarmManager.cancel(pendingIntent);
		alarmManager.set(AlarmManager.RTC, dt.getTime(), pendingIntent);
	}

}
